using System;
using System.IO;
using EnvDTE;

namespace UnitLite.Interface
{
    public class TestContext
    {
        private DTE _DTE;
		private string _TestProjectName = String.Empty;
		private string _TestFolder = TestConstants.DefaultTestFolder;
		private string _TestSuffix = TestConstants.DefaultTestSuffix;
		private SourceCodeInfo _SourceCodeInfo;
		private TestConfigInfo _ConfigInfo;

        public TestContext()
        {

        }

		public TestContext(DTE DTE_)
		{
			_DTE = DTE_;
		}

        public DTE DTE
        {
            get
            {
                return _DTE;
            }

            set
            {
                _DTE = value;
            }
        }

		public Solution Solution
		{
			get
			{
				return _DTE.Solution;
			}
		}

        public Document SourceDocument
        {
            get
            {
                return _DTE.ActiveDocument;
            }
        }

		public Project SourceProject
		{
			get
			{
				return SourceDocument.ProjectItem.ContainingProject;
			}
		}

		public string TestFolderName
		{
			set
			{
				_TestFolder = value;
			}

			get
			{
				return _TestFolder;
			}
		}
	
		public string TestProjectName
		{
			set
			{
				_TestProjectName = value;	
			}

			get
			{
				return _TestProjectName;
			}
		}

		public SourceCodeInfo CodeInfo
		{
			get
			{
				return _SourceCodeInfo;
			}

			set
			{
				_SourceCodeInfo = value;
			}
		}

		public string TestSuffix 
		{
			get
			{
				return _TestSuffix;
			}

			set
			{
				_TestSuffix = value;
			}
		}
		public TestConfigInfo TestConfigInfo
		{
			get
			{
				return _ConfigInfo;
			}

			set
			{
				_ConfigInfo = value;
			}
		}

		public string TestProjectFullName
		{
			get
			{
				return GetTestProjectFullName(SourceProject.FullName, TestProjectName);
			}
		}

		public string TestFileFullName
		{
			get
			{
				return GetTestFileFullName(SourceDocument.FullName, SourceDocument.Name);
			}
		}

		/// <summary>
		/// Get test target project full path.
		/// </summary>
		/// <param name="SourceProjectFullName"></param>
		/// <param name="TestProjectName"></param>
		/// <returns></returns>
		public string GetTestProjectFullName(string SourceProjectFullName, string TestProjectName)
		{
			if (!Validate(SourceProjectFullName, TestProjectName))
			{
				return String.Empty;
			}

            string SourceDirectoryPath = SourceProjectFullName;

            if (!Directory.Exists(SourceDirectoryPath))
            {
                SourceDirectoryPath = Path.GetDirectoryName(SourceProjectFullName);
            }

            string TestProjectDirectoryPath = Path.Combine(SourceDirectoryPath, TestFolderName);
			if (!Directory.Exists(TestProjectDirectoryPath))
			{
				Directory.CreateDirectory(TestProjectDirectoryPath);
			}

			string TestProjectFullName = Path.Combine(TestProjectDirectoryPath, TestProjectName);
			
			if (!TestProjectFullName.EndsWith(TestConstants.ProjectExtension))
			{
				TestProjectFullName += TestConstants.ProjectExtension;			
			}

			return TestProjectFullName;
		}

		/// <summary>
		/// Get test target document full path.
		/// </summary>
		/// <param name="SrcDocFullName">The source file full Path.</param>
		/// <param name="TestDocPath">The target document file name</param>
		/// <returns></returns>
		public string GetTestFileFullName(string SrcDocFullName, string TestDocPath)
		{
			if (!Validate(SrcDocFullName, TestDocPath))
			{
				return String.Empty;
			}

            string SourceDirectoryPath = SrcDocFullName;

            if (!Directory.Exists(SourceDirectoryPath))
            {
                SourceDirectoryPath = Path.GetDirectoryName(SrcDocFullName);
            }

			string TestFileDirectoryPath  = Path.Combine(SourceDirectoryPath, TestFolderName);

			if (!Directory.Exists(TestFileDirectoryPath))
			{
				Directory.CreateDirectory(TestFileDirectoryPath);
			}

			string TestFileFullName = Path.Combine(TestFileDirectoryPath, Path.GetFileNameWithoutExtension(TestDocPath)) + TestSuffix;

			if (!TestFileFullName.EndsWith(TestConstants.FileExtension))
			{
				TestFileFullName += TestConstants.FileExtension;		
			}

			return TestFileFullName;
		}

		internal bool Validate(string FullName, string Name)
		{
			if (FullName == null || FullName == String.Empty)
			{
				return false;
			}

			if (Name == null || Name == String.Empty)
			{
				return false;
			}
			return true;
		}
	
    }
}
